package com.example.dao.impl;

import com.example.dao.OrderRecordDao;
import com.example.entity.customerAdmin.OrderRecord;
import com.example.utils.BaseDao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class OrderRecordDaoImpl implements OrderRecordDao {
    @Override
    public int add (OrderRecord orderRecord) {

        int i=-1;
        try {
            Object[] objects=new Object[6];
            objects[0]=orderRecord.getOrderTitle ();
            objects[1]=orderRecord.getSumMoney ();
            objects[2]=orderRecord.getApprovalStates ();
            objects[3]=orderRecord.getReturnMoney ();
            objects[4]=orderRecord.getOpenMoney ();
            objects[5]=orderRecord.getTime ();

            String sql="insert into orderrecord(ordertitle,summoney,approvalstates,returnmoney,openmoney,time) value(?,?,?,?,?,?)";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int del (int id) {
        int i=-1;
        try {
            Object[] objects=new Object[1];
            objects[0]=id;
            i= BaseDao.modify ("delete from orderrecord where id=?",objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public int update (OrderRecord orderRecord) {
        int i=-1;
        try {
            Object[] objects=new Object[7];
            objects[0]=orderRecord.getOrderTitle ();
            objects[1]=orderRecord.getSumMoney ();
            objects[2]=orderRecord.getApprovalStates ();
            objects[3]=orderRecord.getReturnMoney ();
            objects[4]=orderRecord.getOpenMoney ();
            objects[5]=orderRecord.getTime ();
            objects[6]=orderRecord.getId ();
            String sql="update orderrecord set ordertitle=?,summoney=?,approvalstates=?,returnmoney=?,openmoney=?,time=? where id=?";
            i= BaseDao.modify (sql,objects);
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public List<OrderRecord> selectAll (int page, int limit) {

        List<OrderRecord> orderRecordList=new ArrayList<> ();
        Object[] objects=new Object[2];
        objects[0]=page;
        objects[1]=limit;
        ResultSet select = BaseDao.select ("select * from orderrecord limit ?,?", objects);
        try {
            while (select.next ()){
                OrderRecord orderRecord=new OrderRecord ();
                orderRecord.setId (select.getInt ("id"));
                orderRecord.setOrderTitle (select.getString ("ordertitle"));
                orderRecord.setSumMoney (select.getInt ("summoney"));
                orderRecord.setApprovalStates (select.getString ("approvalstates"));
                orderRecord.setReturnMoney (select.getInt ("returnmoney"));
                orderRecord.setOpenMoney (select.getInt ("openmoney"));
                orderRecord.setTime (select.getString ("time"));
                orderRecordList.add (orderRecord);
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return orderRecordList;
    }

    @Override
    public int getCount () {
        int i=-1;
        ResultSet select = BaseDao.select ("select count(1) getCount from orderrecord ",null);
        try {
            while (select.next ()){
                i=select.getInt ("getCount");
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return i;
    }

    @Override
    public OrderRecord selectId (int id) {
        OrderRecord orderRecord=new OrderRecord ();
        Object[] objects=new Object[1];
        objects[0]=id;
        ResultSet select = BaseDao.select ("select * from orderrecord where id=? ", objects);
        try {
            while (select.next ()){

                orderRecord.setId (select.getInt ("id"));
                orderRecord.setOrderTitle (select.getString ("ordertitle"));
                orderRecord.setSumMoney (select.getInt ("summoney"));
                orderRecord.setApprovalStates (select.getString ("approvalstates"));
                orderRecord.setReturnMoney (select.getInt ("returnmoney"));
                orderRecord.setOpenMoney (select.getInt ("openmoney"));
                orderRecord.setTime (select.getString ("time"));
            }
        }catch (Exception e){
            e.printStackTrace ();
        }
        return orderRecord;
    }
}
